﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DbMapping.Common;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.MVCWeb.Attributes;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        [ActionAuthorize(AuthorizeType.Administrator)]
        public ActionResult Index(string keyword, int page = 1)
        {
            Conditions conditions = new Conditions();
            if (!string.IsNullOrEmpty(keyword))
            {
                conditions.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.StartWith, keyword));
                conditions.Add(new ConditionItem(LinkType.Or, UserInfo.IdField, CompareType.StartWith, keyword));
            }

            int pageSize = 30;
            int recordCount = 0;

            ViewBag.PageSize = pageSize;
            ViewBag.RecordCount = recordCount;
            var list = UserBLL.FindPage(conditions, new Orders(), page, pageSize, ref recordCount);

            return View(list);
        }

        [ActionAuthorize(AuthorizeType.Administrator)]
        public ActionResult Edit(int id)
        {
            UserInfo info = UserBLL.Find(id);

            if (info == null) return HttpNotFound();

            UserModel model = new UserModel();
            model.Id = info.Id;
            model.UserName = info.UserName;
            model.Password = info.Password;
            model.Email = info.Email;

            return View(model);
        }

        [ActionAuthorize(AuthorizeType.Administrator)]
        [HttpPost]
        public ActionResult Edit(UserModel model)
        {
            if (ModelState.IsValid)
            {
                UserInfo info = UserBLL.Find(model.Id);

                if (info == null) return HttpNotFound();

                info.Id = model.Id;
                info.Email = model.Email;
                info.UserName = model.UserName;
                info.Password = model.Password;

                UserBLL.Update(info);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        [ActionAuthorize(AuthorizeType.Administrator)]
        public ActionResult UserRoles(int id)
        {
            UserInfo info = UserBLL.Find(id);

            if (info == null)
            {
                return HttpNotFound();
            }

            UserRoleModel model = new UserRoleModel();
            model.UserId = info.Id;
            model.UserName = info.UserName;

            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserRoleInfo.UserIdField, CompareType.Equal, id));

            var list = UserRoleBLL.FindAll(cons, null);

            foreach (UserRoleInfo userRole in list)
            {
                model.Roles.Add(userRole.RoleId);
            }

            ViewBag.Roles = RoleBLL.FindAll(null, null);

            return View(model);
        }

        [ActionAuthorize(AuthorizeType.Administrator)]
        [HttpPost]
        public ActionResult UserRoles(UserRoleModel model)
        {
            if (ModelState.IsValid)
            {
                RoleBLL.SetUserRoles(model.UserId, model.Roles.ToArray());

                return RedirectToAction("Index", "User", new { area = "Admin" });
            }
            return View(model);
        }

        [ActionAuthorize(AuthorizeType.Administrator)]
        public ActionResult Delete(int id)
        {
            UserBLL.Delete(id);

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult IsUserNameAvaliable(string id, string userName)
        {
            int userId;

            if (!int.TryParse(id, out userId))
            {
                userId = 0;
            }

            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserInfo.IdField, CompareType.NotEqual, userId));
            cons.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.Equal, userName));

            return Json(!UserBLL.Exists(cons));
        }

        [HttpPost]
        public ActionResult IsEmailAvaliable(string id, string email)
        {
            int userId;

            if (!int.TryParse(id, out userId))
            {
                userId = 0;
            }

            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserInfo.IdField, CompareType.NotEqual, userId));
            cons.Add(new ConditionItem(LinkType.And, UserInfo.EmailField, CompareType.Equal, email));

            return Json(!UserBLL.Exists(cons));
        }
    }
}